package net.qiqbframework.modelling.repository;


import net.qiqbframework.modelling.domain.Aggregate;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

/**
 * 聚合仓库
 *
 * @param <A>
 */
public interface Repository<A> {

    /**
     * 批量加载
     *
     * @param bizIdentifierVouchers
     * @return
     */
    Map<AggregateBizIdentifierVoucher, Aggregate<A>> batchLoad(List<AggregateBizIdentifierVoucher> bizIdentifierVouchers);

    /**
     * 从缓冲中加载
     *
     * @param bizIdentifierVoucher
     * @return
     */
    Aggregate<A> loadFromCache(AggregateBizIdentifierVoucher bizIdentifierVoucher);

    Aggregate<A> newInstance(@Nonnull Callable<A> factoryMethod) throws Exception;

    default Aggregate<A> newInstance(Callable<A> factoryMethod, Consumer<Aggregate<A>> initMethod) throws Exception {
        Aggregate<A> aggregate = newInstance(factoryMethod);
        initMethod.accept(aggregate);
        return aggregate;
    }

}
